package com.zerocode.smarttravel.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import java.lang.RuntimeException

/**
 * 基础的 fragment
 * @author YSJ
 * @date 2021/7/13 : 20:09
 */
open abstract class BaseFragment<T : ViewDataBinding> : Fragment() {

    var binding: T? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return getView(savedInstanceState, container)
    }

    private fun getView(savedInstanceState: Bundle?, container: ViewGroup?): View {
        binding = DataBindingUtil.bind(
            LayoutInflater.from(context).inflate(getLayout(savedInstanceState), null)
        )
        initData()
        initEvent()
        return if (binding != null) binding?.root!! else throw RuntimeException("布局文件存在问题")
    }

    abstract fun getLayout(savedInstanceState: Bundle?): Int

    abstract fun initEvent()

    abstract fun initData()


}